package com.webinsky.persist.dao.catalog;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.webinsky.common.utils.db.BasicSqlProvider;
import com.webinsky.common.utils.db.PageHelper;
import com.webinsky.common.utils.db.Pagnation;
import com.webinsky.common.utils.db.SQLBuilder;
import com.webinsky.persist.entity.catalog.TagFilter;

public class TagProvider extends BasicSqlProvider {

	private String[] cols = new String[]{"id", "name", "tag_cat_id", "del", "sort", "language", "create_at", "update_at"};
	private String[] count_col = new String[]{"count(*)"};
	
	private String baseSearchSQL = " select t.id, t.name, t.tag_cat_id, t.del, t.sort, t.language, t.create_at, t.update_at from tag t "
			+ " inner join tagcat tc on tc.id = t.tag_cat_id ";
	
	private String baseCountSearchSQL = " select count(*) from tag t "
			+ " inner join tagcat tc on tc.id = t.tag_cat_id ";
	
	public TagProvider() {
		super("tag");
	}
	
	public String selectByPagnation(Map<String, Object> param){
		Pagnation filter = (Pagnation)param.get("filter");
		SQLBuilder sqlBuilder = getSelectBuilder(cols);
		int pageStart = filter.getPageStart();
		int pageSize = filter.getPageSize();
		String pageSQL = PageHelper.startPage(sqlBuilder.toString(), pageStart, pageSize);
		return pageSQL;
	}
	
	
	public String selectByFilter(Map<String, Object> param){
		TagFilter filter = (TagFilter)param.get("filter");
		SQLBuilder sqlBuilder = getSelectBuilder(cols);
		if ( filter != null ){
			if ( filter.getTagcatId() > 0 ){
				sqlBuilder.WHERE("tag_cat_id = " + filter.getTagcatId());
			}
			if ( filter.getName() != null ){
				sqlBuilder.WHERE("name like '%" + filter.getName() + "%'");
			}
		}
		int pageStart = filter.getPageStart();
		int pageSize = filter.getPageSize();
		String pageSQL = PageHelper.startPage(sqlBuilder.toString(), pageStart, pageSize);
		return pageSQL;
	}
	
	
	public String selectByTagcatIds(List<Integer> tagcatIds){
		SQLBuilder sqlBuilder = getSelectBuilder(cols);
		if ( tagcatIds != null ){
			List<String> strIds = tagcatIds.stream().map(e -> e.toString()).collect(Collectors.toList());
			sqlBuilder.WHERE(" tagcatId in (" + String.join(",", strIds) + ")" );
		}
		return sqlBuilder.toString();
	}
	
	public String selectCountByFilter(Map<String, Object> param){
		TagFilter filter = (TagFilter)param.get("filter");
		SQLBuilder sqlBuilder = getSelectBuilder(count_col);
		if ( filter != null ){
			if ( filter.getTagcatId() > 0 ){
				sqlBuilder.WHERE("tag_cat_id = " + filter.getTagcatId());
			}
			if ( filter.getName() != null ){
				sqlBuilder.WHERE("name like '%" + filter.getName() + "%'");
			}
		}
		return sqlBuilder.toString();
	}
	
	public String selectCountByTagcatFilter(Map<String, Object> param){
		TagFilter filter = (TagFilter)param.get("filter");
		String sql = baseCountSearchSQL;
		sql = sql + " where tc.Del = 0 and t.Del = 0 ";
		if ( filter != null ){
			List<String> conditions = new ArrayList<>();
			if ( filter.getTagcatId() > 0 ){
				conditions.add("t.tag_cat_id = " + filter.getTagcatId());
			}
			if ( filter.getLang() != null ){
				conditions.add("tc.language = " + filter.getLang());
			}
			if ( filter.getSiteId() > 0 ){
				conditions.add("tc.site_id = " + filter.getSiteId());
			}
			if ( filter.getName() != null ){
				conditions.add("t.name like '%" + filter.getName() + "%'");
			}
			
			if ( !conditions.isEmpty() ){
				sql = sql + String.join(" and ", conditions);
			}
		}
		return sql;
	}
	
	public String selectByTagcatFilter(Map<String, Object> param){
		TagFilter filter = (TagFilter)param.get("filter");
		String sql = baseSearchSQL;
		sql = sql + " where tc.Del = 0 and t.Del = 0 ";
		if ( filter != null ){
			List<String> conditions = new ArrayList<>();
			if ( filter.getTagcatId() > 0 ){
				conditions.add("t.tag_cat_id = " + filter.getTagcatId());
			}
			if ( filter.getLang() != null ){
				conditions.add("tc.language = '" + filter.getLang() + "'");
			}
			if ( filter.getSiteId() > 0 ){
				conditions.add("tc.site_id = " + filter.getSiteId());
			}
			if ( filter.getName() != null && filter.getName().length() > 0 ){
				conditions.add("t.name like '%" + filter.getName() + "%'");
			}
			
			if ( !conditions.isEmpty() ){
				sql = sql + " and " + String.join(" and ", conditions);
			}
		}
		int pageStart = filter.getPageStart();
		int pageSize = filter.getPageSize();
		return PageHelper.startPage(sql, pageStart, pageSize);
	}
	
	@SuppressWarnings("unchecked")
	public String selectByIds(Map<String, Object> params){
		List<Integer> ids = (List<Integer>)params.get("ids");
		SQLBuilder sqlBuilder = getSelectBuilder(cols);
		sqlBuilder.WHERE("del = 0");
		if ( ids != null && !ids.isEmpty() ){
			List<String> strIds = ids.stream().map(i -> String.valueOf(i)).collect(Collectors.toList());
			sqlBuilder.WHERE("id in (" + String.join(",", strIds) + ")");
		}
		return sqlBuilder.toString();
	}
}
